package com.app.controller;


import com.app.common.ResponseResult;
import com.app.entity.Course;
import com.app.entity.bo.CourseQueryBo;
import com.app.enums.ResponseEnum;
import com.app.service.CourseCollectionService;
import com.app.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yeyuan
 * @since 2021-12-26
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseCollectionService courseCollectionService;

    @PostMapping("/selectAll")
    public ResponseResult<List<Course>> selectAll(@RequestBody(required = false) CourseQueryBo courseQueryBo){

        List<Course> courses = courseService.selectByQueryBo(courseQueryBo);
        return ResponseResult.success(courses);
    }


    @PostMapping("/addCourse")
    public ResponseResult addOne(@RequestBody Course course){
        int result = courseService.addCourse(course);
        if(result > 0)
            return ResponseResult.success("新增课程成功");
        else return ResponseResult.error(ResponseEnum.COURSE_ADD_FAIL);
    }


    @GetMapping("/isCollected")
    public ResponseResult isCollected(@RequestParam Long cid,@RequestParam Long uid){

        return ResponseResult.success(courseCollectionService.getIsCollected( cid,  uid));
    }

    @PostMapping("/collect")
    public ResponseResult collect(@RequestParam Long cid,@RequestParam Long uid){

        return ResponseResult.success(courseCollectionService.collect( cid,  uid));
    }

    @GetMapping("/cancelCollect")
    public ResponseResult cancelCollect(@RequestParam Long cid,@RequestParam Long uid){

        return ResponseResult.success(courseCollectionService.cancelCollect( cid,  uid));
    }


    @GetMapping("/getCollectedList")
    public ResponseResult<List<Course>> getCollectedList(@RequestParam Long uid){

        List<Course> courses = courseCollectionService.getCollectedListByUid(uid);
        return ResponseResult.success(courses);
    }



}
